import { MiViewOrEditService } from './mi-view-or-edit.service';
import { Component, OnInit } from '@angular/core';
import { DataService } from 'app/services/data.service';
import * as _ from "lodash";
import { Util } from 'app/common/util';

@Component({
  selector: 'app-mi-view-or-edit',
  templateUrl: './mi-view-or-edit.component.html',
  styleUrls: ['./mi-view-or-edit.component.scss'],
  providers: [MiViewOrEditService]
})
export class MiViewOrEditComponent implements OnInit {

  //查看或编辑操作
  public operate;
  //产品内码
  public nRid;
  // 产品信息
  public singleInfo: any = {
    // nRid: this.nRid,
    // sOperpoint:'',//操作点
    // sTitle:'',//摘要
    // sContent: '',//内容
    // sTmpTitle: '',//临时摘要
    // sTmpContent: '',//临时内容
    // sStatus: ''//状态
  }
  // 保存旧数据对比是否修改
  public oldSingleInfo: any = {};

  constructor(
    public MiViewOrEditService: MiViewOrEditService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    // this.GetSingleInfo();
  }

  // 查询数据信息
  // GetSingleInfo(){
  // let that = this;
  // this.MiViewOrEditService.getInfo(this.nRid).subscribe(data => {
  //   if (data) {
  //     that.singleInfo = data;
  //     that.oldSingleInfo = _.clone(that.singleInfo)
  //   }
  // });
  // }

  //保存修改
  updateInfo() {
    let that = this;
    this.singleInfo.sTmpTitle = _.trim(this.singleInfo.sTmpTitle);
    this.singleInfo.sTmpContent = _.trim(this.singleInfo.sTmpContent);
    let putBody: any = {
      nRid: this.singleInfo.nRid,
      sTitle: this.singleInfo.sTitle,//摘要
      sContent: this.singleInfo.sContent,//内容
      sTmpTitle: this.singleInfo.sTmpTitle,//临时摘要
      sTmpContent: this.singleInfo.sTmpContent,//临时内容
      sStatus: this.singleInfo.sStatus,//状态
      sOperpoint: this.singleInfo.sOperpoint
    }
    if (Util.objectCheck(that.singleInfo, that.oldSingleInfo)) {
      window["swal"]("提示", "数据没有修改，无需保存！", "warning");
    } else {
      if (this.requireAdd()) {
        window["swal"]({
          title: "提示",
          text: "是否确认保存？",
          type: "warning",
          confirmButtonText: "确认",
          confirmButtonColor: "#DD6B55",
          cancelButtonText: "取消",
          showCancelButton: true,
          closeOnConfirm: false,
          closeOnCancel: true,
          showLoaderOnConfirm: true
        },
          function (isConfirm) {
            if (isConfirm) {
              that.MiViewOrEditService.updateInfo(putBody).subscribe(data => {
                if (data) {
                  window["swal"]("成功", "保存成功，请等待数据审核!", "success");
                  that.dataService.rcmiEditEmitter.emit('MiViewOrEdit');
                }
              });
            }
          });
      }
    }
  }

  // 必填校验
  requireAdd() {
    let that = this;
    let requiredAddTip = [];
    let sTmpTitle = _.trim(this.singleInfo.sTmpTitle);
    let sContent = _.trim(this.singleInfo.sTmpContent);
    if (sTmpTitle == "" || sTmpTitle == null) {
      requiredAddTip.push("须知摘要");
    }
    if (sContent == "" || sContent == null) {
      requiredAddTip.push("须知内容");
    }
    if (requiredAddTip.length > 0) {
      let requiredAddTips = requiredAddTip.join("、");
      window['swal']("提示", "请填写:" + requiredAddTips, "warning");
      return false;
    } else {
      return true;
    }
  }

}
